﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace ChMetody_2
{
    public partial class Form1 : Form
    {
        //Хранение матрицы. Ваня
        private Matrix M1;
        private List<double> L1;
        private List<double> solution;
        public Form1()
        {
            InitializeComponent();
        }
        /// <summary>
        /// Очистка таблиц
        /// </summary>
        /// <param name="d1">сама таблица(DataGridView)</param>
        /// <param name="n">размер матрицы</param>
        public void clear_table(System.Windows.Forms.DataGridView d1, int n, int m = 0)
        {
            if (m == 0) m = n;
            for (int i = 0; i < n; i++)
                for (int j = 0; j < m; j++)
                    d1.Rows[i].Cells[j].Value = "";
        }

        /// <summary>
        /// Настройка таблицы
        /// </summary>
        /// <param name="d1">Ссылка на таблицу</param>
        /// <param name="n">Количество строк</param>
        /// <param name="m">Количество столбцов (необязательно если квадратная)</param>
        public void set_grid(System.Windows.Forms.DataGridView d1, int n, int m = 0)
        {
            if (m == 0) m = n;
            d1.ColumnCount = m;
            d1.RowCount = n;
            for (int i = 0; i < n; i++)
            {
                d1.Rows[i].Height = 25;
                d1.Rows[i].HeaderCell.Value = (i + 1).ToString();
            }
            for (int i = 0; i < m; i++)
            {
                d1.Columns[i].Width = 30;
                d1.Columns[i].HeaderText = (i + 1).ToString();
            }
        }

        /// <summary>
        /// Заполняет таблицу n*n случайными числами
        /// </summary>
        /// <param name="d1">Ссылка на таблицу</param>
        /// <param name="n">Размерность таблицы</param>
        /// <param name="m">Количество столбцов (необязательно если квадратная)</param>
        public void randomize(System.Windows.Forms.DataGridView d1, int n, int m = 0)
        {
            int k = n * 5;
            if (m == 0) m = n;
            Random rand = new Random();
            for (int i = 0; i < n; i++)
                for (int j = 0; j < m; j++)
                    d1.Rows[i].Cells[j].Value = rand.Next(k).ToString();
        }

        //настройка таблицы Вити
        private void textBox2_TextChanged(object sender, EventArgs e)
        {
            if (textBox2.Text != "")
            {
                int n = Convert.ToInt32(textBox2.Text);
                set_grid(dataGridView2, n,n+1);
            }
        }

        private void textBox3_TextChanged(object sender, EventArgs e)
        {
            if (textBox3.Text != "")
            {
                int n = Convert.ToInt32(textBox3.Text);
                set_grid(dataGridView3, n);
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (textBox3.Text != "")
            {
                int n = Convert.ToInt32(textBox3.Text);
                set_grid(dataGridView4, n);
                for (int i = 0; i < n; i++)
                    for (int j = 0; j < n; j++)
                        dataGridView4.Rows[i].Cells[j].Value = "";
                double[,] Q = new double[n, n];
                double[,] X = new double[n, n];
                for (int i = 0; i < n; ++i)
                    for (int j = 0; j < n; ++j)
                        Q[i,j] = Convert.ToDouble(dataGridView3.Rows[i].Cells[j].Value);
                
                bool f = Gaus.ObratM(ref Q,ref X,0.00000000001, n);

                if (f == true)
                {
                    for (int i = 0; i < n; ++i)
                        for (int j = 0; j < n; ++j)
                            dataGridView4.Rows[i].Cells[j].Value = X[i, j];
                }
                else
                {
                    System.Windows.Forms.MessageBox.Show("Невозможно найти обратную матрицу.");
                }
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            if (radioButton7.Checked == true && textBox2.Text != "")
            {                
                int n = Convert.ToInt32(textBox2.Text);
                randomize(dataGridView2, n,n+1);
                randomize(dataGridView2, 1, n);
            }

            if (radioButton8.Checked == true)
            {
                Methods.read_file("input.txt");
                textBox2.Text = Methods.count.ToString();
                for (int i = 0; i < Methods.count; i++)
                    for (int j = 0; j < Methods.count; j++)
                        dataGridView2.Rows[i].Cells[j].Value = Methods.slr[i, j].ToString();
            }
        }

        private void button4_Click(object sender, EventArgs e)
        {
            if (textBox2.Text != "")
            {
                int n = Convert.ToInt32(textBox2.Text);
                clear_table(dataGridView2, n);
                for (int i = 0; i < n; i++)
                {
                    if (dataGridView6.ColumnCount != 0)
                    {
                        dataGridView6.Rows[0].Cells[i].Value = "";
                        dataGridView7.Rows[0].Cells[i].Value = "";
                    }
                    dataGridView2.Rows[i].Cells[n].Value = "";
                }
                label7.Text = "|R| = ";
            }    
        }

        private void button5_Click(object sender, EventArgs e)
        {
            if (radioButton6.Checked == true && textBox3.Text != "")
            {
                int n = Convert.ToInt32(textBox3.Text);
                randomize(dataGridView3, n);
            }
            if (radioButton9.Checked == true)
            {
                Methods.read_file("input.txt");
                textBox3.Text = Methods.count.ToString();
                for (int i = 0; i < Methods.count; i++)
                    for (int j = 0; j < Methods.count; j++)
                        dataGridView3.Rows[i].Cells[j].Value = Methods.slr[i, j].ToString();
            }
        }

        //DataGridView3 clear
        private void button6_Click(object sender, EventArgs e)
        {
            if (textBox3.Text != "")
            {
                int n = Convert.ToInt32(textBox3.Text);
                clear_table(dataGridView3, n);
            }
        }
        //DataGridView4 clear   
        private void button7_Click(object sender, EventArgs e)
        {       
            int n = dataGridView4.RowCount;
            for (int i = 0; i < n; i++)
                for (int j = 0; j < n ; j++)
                    dataGridView4.Rows[i].Cells[j].Value = "";
                
        }

        //Настройка таблицы Ваня
        private void textBox1_TextChanged(object sender, EventArgs e)
        {
            if (textBox1.Text != "")
            {
                int n = Convert.ToInt32(textBox1.Text);
                set_grid(dataGridView1, n);
                set_grid(dataGridView5, 1, n);
            }
        }

        private void button8_Click(object sender, EventArgs e)
        {
            solution = Kramer.solution(M1, L1);
            set_grid(dataGridView8, 1, L1.Count);
            for (int i = 0; i < solution.Count; i++)
            {
                dataGridView8.Rows[0].Cells[i].Value = solution[i].ToString();
                dataGridView8.Columns[i].HeaderCell.Value = "x" + i.ToString();
            }
        }

        //Ввод матрицы (вручную/случайно/файл)
        private void button9_Click(object sender, EventArgs e)
        {
            int n = 0;
            if (textBox1.Text != "")
            {
                n = Convert.ToInt32(textBox1.Text);
                M1 = new Matrix(n);
                L1 = new List<double>(n);
            }
            if (radioButton2.Checked == true)
            {
                randomize(dataGridView1, n);
                for (int i = 0; i < n; i++)
                    for (int j = 0; j < n; j++)
                    {
                        M1.Rows[i].Cells.Add(Convert.ToInt32(dataGridView1.Rows[i].Cells[j].Value));
                    }
                randomize(dataGridView5, 1, n);
                for (int j = 0; j < n; j++)
                {
                    L1.Add(Convert.ToInt32(dataGridView5.Rows[0].Cells[j].Value));
                }
            }

            if (radioButton1.Checked == true)
            {
                n = Convert.ToInt32(textBox1.Text);
                for (int i = 0; i < n; i++)
                    for (int j = 0; j < n; j++)
                    {
                        M1.Rows[i].Cells.Add( Convert.ToInt32(dataGridView1.Rows[i].Cells[j].Value));
                    }
                for (int j = 0; j < n; j++)
                {
                    L1.Add(Convert.ToInt32(dataGridView5.Rows[0].Cells[j].Value));
                }
            }

            if (radioButton3.Checked == true)
            {
                Methods.read_file("kramer.txt");
                textBox1.Text = Methods.count.ToString();
                M1 = new Matrix(Methods.count);
                for (int i = 0; i < Methods.count; i++)
                    for (int j = 0; j < Methods.count; j++)
                        dataGridView1.Rows[i].Cells[j].Value = Methods.slr[i, j].ToString();
                for (int i = 0; i < Methods.count; i++)
                    for (int j = 0; j < Methods.count; j++)
                    {
                        M1.Rows[i].Cells.Add(Convert.ToInt32(dataGridView1.Rows[i].Cells[j].Value));
                    }
            }
        }

        private void tabPage1_Click(object sender, EventArgs e)
        {
        }

        private void button1_Click(object sender, EventArgs e)
        {
            int n = Convert.ToInt32(textBox2.Text);
            double[,] A = new double[n,n];
            for (int i=0; i<n; i++)
                for (int j=0; j<n; j++)
                    A[i,j]=Convert.ToDouble(dataGridView2.Rows[i].Cells[j].Value);          
            double[] b = new double[n];
            double[] x = new double[n];
            for (int i = 0; i < n; i++)
                b[i] = Convert.ToDouble(dataGridView2.Rows[i].Cells[n].Value);
            double eps = Math.Pow(10, -11);     
            bool temp = Gaus.Gauss(ref A, ref b,ref x,eps, n);
            if (temp)
            {
                double[] R = new double[n];
                double abs_R;
                Gaus.Check(ref R, out abs_R, A, b, x, n);
                dataGridView6.ColumnCount = n;
                dataGridView6.RowCount = 1;
                dataGridView6.Rows[0].Height = 25;
                dataGridView7.ColumnCount = n;
                dataGridView7.RowCount = 1;
                dataGridView7.Rows[0].Height = 25;
                for (int i = 0; i < n; i++)
                {
                    dataGridView6.Columns[i].Width = 40;
                    dataGridView6.Columns[i].HeaderText = (i + 1).ToString();
                    dataGridView6.Rows[0].Cells[i].Value = String.Format("{0:G3}", x[i]);
                    dataGridView7.Columns[i].Width = 40;
                    dataGridView7.Columns[i].HeaderText = (i + 1).ToString();
                    dataGridView7.Rows[0].Cells[i].Value = String.Format("{0:G3}", R[i]);
                }
                label7.Text = label7.Text + abs_R.ToString();
            }
            else
                MessageBox.Show("Матрица вырожденная!!");
        }
                            
    }
}
